var areaData = address;
var $form;
var form;
var $;
layui.config({
    base: "../../js/"
}).use(['form', 'layer', 'upload', 'laydate'], function () {
    form = layui.form();
    var layer = parent.layer === undefined ? layui.layer : parent.layer;
    $ = layui.jquery;
    $form = $('form');
    laydate = layui.laydate;
    loadProvince(); //加载省信息


    var filedir = "coupon/"; // 对应oss目录

    // 封面图上传是否达1张
    function checkImgUpload(){
        if(imgUrlArr.length < 1){
            top.layer.alert("剧照写真必须上传至少一张",{icon:0});
            return false;
        }
        return true;
    }


    initCoverImgUpload();

    // 剧照写真大小上传初始化
    var imgUrlArr = [];
    function initCoverImgUpload() {
        $.Tupload.init({
            url: '/oss/ossImgUpload?filedir='+filedir,
            title: "写真大小不能超过300kb<span style='color:red;'></span>",
            fileNum: 5, // 上传文件数量
            divId: "T_upload", // div  id
            accept: "image/jpeg,image/x-png,image/jpg", // 上传文件的类型
            fileSize:300,     // 上传文件的大小 kb
            filedir:filedir, // 图片上传至oss对应的目录
            onSuccess: function(data, i) {
                console.log(i+">>>>>>>>"+JSON.stringify(data));
                imgUrlArr[i] = data["link"];
                $("#portraitUrl").val(imgUrlArr.join("@@@@"));
            },
            onDelete: function(i) {

            }
        });
    }
    $('#resetBtn').click(function(){
        window.location.reload();
    });

    /* upload({
        url: "../../json/userface.json",
        success: function (res) {
            var num = parseInt(4 * Math.random());  //生成0-4的随机数
            //随机显示一个头像信息
            userFace.src = res.data[num].src;
            window.sessionStorage.setItem('userFace', res.data[num].src);
        }
    });

    //判断是否修改过头像，如果修改过则显示修改后的头像，否则显示默认头像
   if (window.sessionStorage.getItem('userFace')) {
        $("#userFace").attr("src", window.sessionStorage.getItem('userFace'));
    } else {
        $("#userFace").attr("src", "../../images/face.jpg");
    }*/
    layui.upload({
    url: '/oss/ossImgUpload?filedir='+filedir,
        before: function(input){
        //返回的参数item，即为当前的input DOM对象
        console.log('文件上传中');
    },
    success: function(res){
        console.log('上传完毕');
        console.log(">>>>>>>>>>>>>>>"+JSON.stringify(res));
        $("#actorFace").attr("src",res.link);
        $("#faceurl").val(res.link);
    }
});
    //提交个人资料
    form.on("submit(submitCoupon)", function (data) {
        // var index = layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //将填写的用户信息存到session以便下次调取
        // if(!checkImgUpload()) return false;
        var data = $("#couponAddForm").serialize();

        $.ajax({
            cache: false,
            type: 'POST',
            url: "/coupon/addCoupon",
            dataType: "json",
            data: data,
            async: false,
            success: function (data) {
                if (data.errType === "0000") {
                    top.layer.alert("保存成功", {icon: 1});
                    setTimeout(function () {
                        var pIndex = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(pIndex); //再执行关闭
                        parent.window.location.reload();
                    },1000)

                } else {
                    top.layer.alert("保存异常,请重试", {icon: 0});
                }
            },
            error: function () {
                top.layer.alert("发生异常,请重试", {icon: 2});
            }
        });
        return false;
    });

    form.on('select(browseLook)', function(data){
        $(':input[name="tName"]').val(data.elem[data.elem.selectedIndex].text);
    });

});

//加载省数据
function loadProvince() {
    var proHtml = '';
    for (var i = 0; i < areaData.length; i++) {
        proHtml += '<option value="' + areaData[i].provinceCode + '_' + areaData[i].mallCityList.length + '_' + i + '">' + areaData[i].provinceName + '</option>';
    }
    //初始化省数据
    $form.find('select[name=province]').append(proHtml);
    form.render();
    form.on('select(province)', function (data) {
        $form.find('select[name=area]').html('<option value="">请选择县/区</option>');
        var value = data.value;
        var d = value.split('_');
        var code = d[0];
        var count = d[1];
        var index = d[2];
        if (count > 0) {
            loadCity(areaData[index].mallCityList);
        } else {
            $form.find('select[name=city]').attr("disabled", "disabled");
        }
    });
}

//加载市数据
function loadCity(citys) {
    var cityHtml = '<option value="">请选择市</option>';
    for (var i = 0; i < citys.length; i++) {
        cityHtml += '<option value="' + citys[i].cityCode + '_' + citys[i].mallAreaList.length + '_' + i + '">' + citys[i].cityName + '</option>';
    }
    $form.find('select[name=city]').html(cityHtml).removeAttr("disabled");
    form.render();
    form.on('select(city)', function (data) {
        var value = data.value;
        var d = value.split('_');
        var code = d[0];
        var count = d[1];
        var index = d[2];
        if (count > 0) {
            loadArea(citys[index].mallAreaList);
        } else {
            $form.find('select[name=area]').attr("disabled", "disabled");
        }
    });
}

//加载县/区数据
function loadArea(areas) {
    var areaHtml = '<option value="">请选择县/区</option>';
    for (var i = 0; i < areas.length; i++) {
        areaHtml += '<option value="' + areas[i].areaCode + '">' + areas[i].areaName + '</option>';
    }
    $form.find('select[name=area]').html(areaHtml).removeAttr("disabled");
    form.render();
    form.on('select(area)', function (data) {
    });
}
